<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  </head>
  <body>
  
  	<!-- grid -->
  	<table id="treegrid"></table>
    
    <!-- 工具条 -->
    <div id="toolbar">
    	<shiro:hasPermission name="Perm:add"> 
    		<a href="#" class="easyui-linkbutton" iconCls="database_add" plain="true" onclick="addPerm()">增加</a>
    	</shiro:hasPermission>
    	<shiro:hasPermission name="Perm:update"> 
    		<a href="#" class="easyui-linkbutton" iconCls="database_edit" plain="true" onclick="editPerm()">修改</a>
    	</shiro:hasPermission>
    	<shiro:hasPermission name="Perm:delete"> 
    		<a onclick="delPerm();" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'database_delete'">删除</a>
    	</shiro:hasPermission>
        
        <a onclick="permTree.treegrid('reload');" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon_refresh'">刷新</a>
    </div>
	
  </body>
  
 	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/resources/js/themes/default/easyui.css" />
	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/resources/js/themes/icon.css" />
    <script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/jquery-1.8.3.js"></script>
	<script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript" src="<%=request.getContextPath()%>/resources/js/extEasyUI.js"></script>
	<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()%>/resources/css/extEasyUIIcon.css" />
	<script type="text/javascript">
		var permTree;
		
		jQuery(function(){
			permTree = jQuery('#treegrid').treegrid({
			    url:'<%=request.getContextPath() %>/perm/getPermList',
			    title:'资源列表',
			    idField:'id',
			    treeField:'name',
			    parentField:'pid',
			    rownumbers:true,
			    toolbar:'#toolbar',
			    columns:[[
			        {field:'name',title:'资源名称'},
			        {field:'type',title:'资源类型',formatter:function(v,r,i){
			        	if (v == "1") return "菜单";
			        	else if (v == "2") return "按钮";
			        	else return "外部菜单";
			        }},
			        {field:'path',title:'资源路径'},
			        {field:'permission',title:'权限标志'},
			        {field:'enable',title:'是否启用',formatter:function(v,r,i){
			        	return v=='true' ? "是" : "否";
			        }},
			        {field:'orders',title:'排序'},
			        {field:'description',title:'描述'}
			    ]],
			    onLoadSuccess:function() {
			    	//默认折叠所有节点，效率低
					//jQuery(this).treegrid('collapseAll');
				}
			});
		})
		
		function delPerm(){
			var selectNode = jQuery("#treegrid").treegrid("getSelected");
			
			if (selectNode == null){
				parent.$.messager.alert('提示', '请点击一行选择您要删除的资源。', 'info');
				return false;
			}
			
			var children = jQuery("#treegrid").treegrid("getChildren",selectNode.id);
			
			if (children.length > 0){
				parent.$.messager.alert('提示', '请先删除该资源下子级节点。', 'info');
				return false;
			}
			
			if (selectNode.type != "3"){
				parent.$.messager.alert('提示', '系统内置功能菜单和按钮不能删除。', 'info');
				return false;
			}
			
			parent.$.messager.confirm('询问', '您是否要删除当前资源？', function(b) {
				if (b) {
					openProgress();
					$.post('<%=request.getContextPath() %>/perm/delPerm', {
						id : selectNode.id
					}, function(data) {
						parent.$.messager.progress('close');
						
						if (data.result) {
							permTree.treegrid('reload');
							parent.$.messager.alert('提示', '删除成功', 'info');
						}
					}, 'JSON');
				}
			});
		}
		
		function editPerm(){
			var selectNode = jQuery("#treegrid").treegrid("getSelected");
			
			if (selectNode == null){
				parent.$.messager.alert('提示', '请点击一行选择您要修改的资源。', 'info');
				return false;
			}
			
			if (selectNode.type == '2'){
				parent.$.messager.alert('提示', '系统功能按钮不可修改。', 'info');
				return false;
			}
			
			parent.$.modalDialog({
				title : '修改资源',
				width : 440,
				height : 320,
				href : '<%=request.getContextPath() %>/perm/editPerm?id='+selectNode.id,
				buttons:[{
					text : '保存',
					iconCls:'icon-ok',
					handler:function(){
						parent.$.modalDialog.permTree = permTree;
						parent.$.modalDialog.handler.find('#form').submit();
					}
				},{
					text : '取消',
					iconCls:'icon-cancel',
					handler:function(){
						parent.$.modalDialog.handler.dialog('close');
					}
				}]
			});
		}
		
		
		function addPerm(){
			parent.$.modalDialog({
				title : '增加资源',
				width : 440,
				height : 290,
				href : '<%=request.getContextPath() %>/perm/addPerm',
				buttons:[{
					text : '保存',
					iconCls:'icon-ok',
					handler:function(){
						parent.$.modalDialog.permTree = permTree;
						parent.$.modalDialog.handler.find('#form').submit();
					}
				},{
					text : '取消',
					iconCls:'icon-cancel',
					handler:function(){
						parent.$.modalDialog.handler.dialog('close');
					}
				}]
			});
		}
	</script>
</html>
